package com.sj.purchase.modules.cost.config.task.compute;

import com.sj.purchase.modules.cost.domain.StockContrast;
import com.sj.purchase.modules.cost.domain.WarehouseEnum;
import com.sj.purchase.modules.cost.repository.StockContrastRepository;
import com.sj.purchase.modules.cost.repository.stock.SkuStockRepository;
import com.sj.purchase.modules.cost.service.stock.SkuStockReportService;
import com.sj.purchase.modules.cost.views.stock.SkuStockReportVO;
import com.sj.purchase.modules.purchase.domain.PurchaseId;
import com.sj.purchase.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Alin on 2017/10/27.
 */
@Component
class StockContrastTask {

    @Autowired
    private SkuStockRepository skuStockRepository;

    @Autowired
    private SkuStockReportService skuStockReportService;

    @Autowired
    private StockContrastRepository stockContrastRepository;

    public Boolean contrast(Date day) {
        day = getDate(day);
        try {
            String sku = "";
            Date endDate =  DateUtils.addDays(day,1);//今天的期初
            Date startDate = day;//昨天的日期 用来计算期末
            Page<String> skuNoPage = skuStockRepository.findBaseStockSku(sku + "%", endDate, null);//不分页
            List<String> skuNoList = skuNoPage.getContent();
            if (!skuNoList.isEmpty()) {
                //昨天的期初、入库、出库、库存调整 相加减的结果 = 昨天的期末
                List<SkuStockReportVO> guangzhou = skuStockReportService.search(WarehouseEnum.GUANGZHOUZC, startDate, endDate, skuNoList);//期末
                List<SkuStockReportVO> jinan = skuStockReportService.search(WarehouseEnum.JINANZC, startDate, endDate, skuNoList);//期末
                List<SkuStockReportVO> shanghai = skuStockReportService.search(WarehouseEnum.SHANGHAI, startDate, endDate, skuNoList);//期末
                List<SkuStockReportVO> shouhou = skuStockReportService.search(WarehouseEnum.SHOUHOU, startDate, endDate, skuNoList);//期末

                Map<String, SkuStockReportVO> gzMap = guangzhou.stream().collect(Collectors.toMap(skuAndNum -> (String) skuAndNum.getSkuNO(), skuAndNum -> skuAndNum));
                Map<String, SkuStockReportVO> jnMap = jinan.stream().collect(Collectors.toMap(skuAndNum -> (String) skuAndNum.getSkuNO(), skuAndNum -> skuAndNum));
                Map<String, SkuStockReportVO> shMap = shanghai.stream().collect(Collectors.toMap(skuAndNum -> (String) skuAndNum.getSkuNO(), skuAndNum -> skuAndNum));
                Map<String, SkuStockReportVO> shouhouMap = shouhou.stream().collect(Collectors.toMap(skuAndNum -> (String) skuAndNum.getSkuNO(), skuAndNum -> skuAndNum));

                List<Object[]> todayBeginStock = skuStockRepository.findBeginStock(endDate, skuNoList);
                Map<String, Long> qichu = todayBeginStock.stream().collect(Collectors.toMap(skuAndNum -> (String) skuAndNum[0], skuAndNum -> (Long) skuAndNum[1]));
                //昨天的期末 - 今天的期初
                List<StockContrast> stockList = skuNoList.stream().map(skuNo -> {
                    //昨日期末
                    Long endNum = 0L;
                    String skuName = "";
                    if (gzMap.get(skuNo) != null) {
                        endNum = gzMap.get(skuNo).getEndStock();
                        skuName = gzMap.get(skuNo).getSkuName();
                    }
                    if (jnMap.get(skuNo) != null) {
                        endNum += jnMap.get(skuNo).getEndStock();
                        skuName = gzMap.get(skuNo).getSkuName();
                    }
                    if (shMap.get(skuNo) != null) {
                        endNum += shMap.get(skuNo).getEndStock();
                        skuName = gzMap.get(skuNo).getSkuName();
                    }
                    if (shouhouMap.get(skuNo) != null) {
                        endNum += shouhouMap.get(skuNo).getEndStock();
                        skuName = gzMap.get(skuNo).getSkuName();
                    }
                    //今天期初库存
                    Long beginNum = 0L;

                    if (qichu.get(skuNo)!=null){
                        beginNum = qichu.get(skuNo);
                    }

                    StockContrast stock = null;
                    if (endNum - beginNum != 0) {
                        stock = new StockContrast();
                        PurchaseId id = new PurchaseId();
                        id.setCreateDate(startDate);
                        id.setSkuNo(skuNo);
                        stock.setSkuName(skuName).setBeginStock(beginNum).setEndStock(endNum).setId(id).setNum(endNum - beginNum);
                    }
                    return stock;
                }).collect(Collectors.toList());
                //remove null
                List<StockContrast> templist = new ArrayList<>();
                for (StockContrast value : stockList) {
                    if (value == null) {
                        templist.add(value);
                    }
                }
                stockList.removeAll(templist);
                stockContrastRepository.save(stockList);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取当前日期的00:00:00
     * @param date
     * @return
     */
    private Date getDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = simpleDateFormat.format(date);
        try {
            date = simpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
